package com.kmxd.ams.controller.arrange;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.archive.dto.BoxGroupFieldQueryDTO;
import com.kmxd.ams.client.archive.dto.RuleFieldQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveRuleFieldListVO;
import com.kmxd.ams.client.arrange.IArrangeCommonSvc;
import com.kmxd.ams.client.arrange.dto.*;
import com.kmxd.ams.client.arrange.vo.*;
import com.kmxd.ams.client.oss.vo.EfileRecordListVO;
import com.kmxd.ams.core.common.enums.ArchOpEnum;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.ArchLogParameter;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.arrange.base.BaseBO;
import com.kmxd.ams.infra.arrange.base.SystemBO;
import com.kmxd.ams.infra.arrange.entity.Box;
import com.kmxd.ams.infra.arrange.entity.Catalog;
import com.kmxd.ams.infra.arrange.entity.Document;
import com.kmxd.ams.infra.arrange.entity.Volume;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 档案整理公共接口 前端控制器
 *
 * @author ZuoLG
 * @since 2022-07-20
 */
@RestController
@Tag(name = "档案整理公共接口")
@RequestMapping("/arrange/common")
@AllArgsConstructor
public class ArrangeCommonCtl {

  private IArrangeCommonSvc arrangeCommonSvc;

  /** 查询档案库树 */
  @Operation(summary = "查询档案库树")
  @GetMapping("/selectArchTree")
  @SaCheckLogin
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "查询档案库树")
  public R<List<Tree<String>>> selectArchTree(QueryArchTypeDTO req) {
    req.setFondsId(
        ObjectUtil.isNotEmpty(req.getFondsId()) ? req.getFondsId() : SecurityUtil.getFondsId());
    return R.success(arrangeCommonSvc.selectArchTree(req));
  }

  /** 档案整理 */
  @Operation(summary = "获取表格列信息")
  @GetMapping("/getClumns")
  @SaCheckLogin
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "获取表格列信息")
  public R<List<ClumnsListVO>> getClumns(BaseQueryDTO req) {
    return R.success(arrangeCommonSvc.getClumns(req));
  }

  /** 获取盒级列信息 */
  @Operation(summary = "获取盒级列信息")
  @GetMapping("/getClumnsByBox")
  @SaCheckLogin
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "获取盒级列信息")
  public R<ClumnsBoxListVO> getClumnsByBox(BaseQueryDTO req) {
    return R.success(arrangeCommonSvc.getClumnsByBox(req));
  }

  /** 获取案卷列信息 */
  @Operation(summary = "获取案卷列信息")
  @GetMapping("/getClumnsByVolume")
  @SaCheckLogin
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "获取案卷列信息")
  public R<ClumnsVolumeListVO> getClumnsByVolume(BaseQueryDTO req) {
    return R.success(arrangeCommonSvc.getClumnsByVolume(req));
  }

  /** 档案整理V2 */
  @Operation(summary = "获取表格列信息V2")
  @GetMapping("/V2/getClumns")
  @SaCheckLogin
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "获取表格列信息V2")
  public R<List<ClumnsListVO>> getClumnsV2(BaseQueryDTO req) {
    return R.success(arrangeCommonSvc.getClumnsV2(req));
  }

  /** 获取盒级列信息V2 */
  @Operation(summary = "获取盒级列信息V2")
  @GetMapping("/V2/getClumnsByBox")
  @SaCheckLogin
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "获取盒级列信息V2")
  public R<ClumnsBoxListVO> getClumnsByBoxV2(BaseQueryDTO req) {
    return R.success(arrangeCommonSvc.getClumnsByBoxV2(req));
  }

  /** 获取案卷列信息V2 */
  @Operation(summary = "获取案卷列信息V2")
  @GetMapping("/V2/getClumnsByVolume")
  @SaCheckLogin
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "获取案卷列信息V2")
  public R<ClumnsVolumeListVO> getClumnsByVolumeV2(BaseQueryDTO req) {
    return R.success(arrangeCommonSvc.getClumnsByVolumeV2(req));
  }

  /** 获取个性化查询设置 */
  @Operation(summary = "获取个性化查询设置")
  @GetMapping("/getClumnsByQuery")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "获取个性化查询设置")
  @SaCheckLogin
  public R<List<ClumnsQueryVO>> getClumnsByQuery(BaseQueryDTO req) {
    return R.success(arrangeCommonSvc.getClumnsByQuery(req));
  }

  /** 档案整理 */
  @Operation(summary = "获取著录界面对象信息")
  @GetMapping("/getFormSchema")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "获取著录界面对象信息")
  @SaCheckLogin
  public R<FormVO> getFormSchema(BaseQueryDTO req) {
    return R.success(arrangeCommonSvc.getFormSchema(req));
  }

  /** 档案整理 */
  @Operation(summary = "获取调整分组对象信息")
  @GetMapping("/getGroupFormSchema")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "获取调整分组对象信息")
  @SaCheckLogin
  public R<List<FormSchemaVO>> getGroupFormSchema(BaseQueryDTO req) {
    return R.success(arrangeCommonSvc.getGroupFormSchema(req));
  }

  /** 获取表单字段 */
  @Operation(summary = "获取表单字段")
  @GetMapping("/getFormField")
  @SaCheckLogin
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "获取表单字段")
  public R<List<FormFieldVO>> getFormField(BaseQueryDTO req) {
    return R.success(arrangeCommonSvc.getFormField(req));
  }

  /** 装盒前校验 */
  @Operation(summary = "装盒|等前置校验")
  @PostMapping("/verify")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "装盒|等前置校验")
  @SaCheckPermission("arrange:common:verify")
  public R<Boolean> verify(@Validated @RequestBody VerifyDTO req) {
    return R.success(arrangeCommonSvc.verify(req));
  }

  /** 重复校验 */
  @Operation(summary = "重复校验")
  @PostMapping("/checkRepeat")
  @SaCheckPermission("arrange:common:checkRepeat")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "重复校验")
  public R<CheckRepeatVO> checkRepeat(@Validated @RequestBody Document req) {
    return R.success(arrangeCommonSvc.checkRepeat(req, null));
  }

  /** 加载盒号信息 */
  @Operation(summary = "加载盒号信息")
  @PostMapping("/loadBox")
  @SaCheckPermission("arrange:common:loadBox")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "加载盒号信息")
  public R<LoadBoxVO> loadBox(@Validated @RequestBody LoadBoxDTO req) {
    return R.success(arrangeCommonSvc.loadBox(req));
  }

  /** 加载盒列表 */
  @Operation(summary = "加载盒列表")
  @PostMapping("/loadBoxList")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "加载盒列表")
  public R<Page<Box>> loadBoxList(Page reqPage, @Validated @RequestBody LoadBoxDTO req) {
    return R.success(arrangeCommonSvc.loadBoxList(reqPage, req));
  }

  /** 装盒 */
  @Operation(summary = "装盒")
  @PostMapping("/inBox")
  @SaCheckPermission("arrange:common:inBox")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "装盒")
  public R<List<InBoxVO>> inBox(@Validated @RequestBody InBoxDTO req) {
    return R.success(arrangeCommonSvc.inBox(req));
  }

  /** 获取档号 */
  @Operation(summary = "获取档号")
  @PostMapping("/archCode")
  @SaCheckPermission("arrange:common:archCode")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "获取档号")
  @ArchLogParameter(op = ArchOpEnum.ARCH_CODE_GENERATE, content = "档号生成")
  public R<ArchCodeVO> archCode(@Validated @RequestBody Document req) {
    return R.success(arrangeCommonSvc.archCode(req));
  }

  /** 刷新档号 */
  @Operation(summary = "刷新档号")
  @PostMapping("/refreshArchCode")
  @SaCheckPermission("arrange:common:refreshArchCode")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "刷新档号")
  public R<Boolean> refreshArchCode(@Validated @RequestBody RefreshArchCodeDTO req) {
    return R.success(arrangeCommonSvc.refreshArchCode(req));
  }

  /** 获取档号 */
  @Operation(summary = "获取案卷档号")
  @PostMapping("/volumeArchCode")
  @SaCheckPermission("arrange:common:volumeArchCode")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "获取档号")
  @ArchLogParameter(op = ArchOpEnum.UPDATE_ARCH_CODE)
  public R<ArchCodeVO> volumeArchCode(@Validated @RequestBody Volume req) {
    return R.success(arrangeCommonSvc.archCode(req));
  }

  /** 归档 */
  @Operation(summary = "归档")
  @PostMapping("/archiving")
  @SaCheckPermission("arrange:common:archiving")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "归档")
  public R<Boolean> archiving(@Validated @RequestBody ArchivingDTO req) {
    return R.success(arrangeCommonSvc.archiving(req));
  }

  /** 重新整理 */
  @Operation(summary = "重新整理")
  @PostMapping("/againCollect")
  @SaCheckPermission("arrange:common:againCollect")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "重新整理")
  public R<Boolean> againCollect(@Validated @RequestBody AgainCollectDTO req) {
    return R.success(arrangeCommonSvc.againCollect(req));
  }

  /** 调整盒信息 */
  @Operation(summary = "调整盒信息")
  @PostMapping("/adjustGroup")
  @SaCheckPermission("arrange:common:adjustGroup")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "调整盒信息")
  public R<Boolean> adjustGroup(@Validated @RequestBody AdjustGroupDTO req) {
    return R.success(arrangeCommonSvc.adjustGroup(req));
  }

  /** 修改字段 */
  @Operation(summary = "修改字段")
  @PostMapping("/updateField")
  @SaCheckPermission("arrange:common:updateField")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "修改字段")
  public R<Boolean> updateField(@Validated @RequestBody UpdateFieldDTO req) {
    return R.success(arrangeCommonSvc.updateField(req));
  }

  /** 组卷 */
  @Operation(summary = "组卷")
  @PostMapping("/inVolume")
  @SaCheckPermission("arrange:common:inVolume")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "组卷")
  @ArchLogParameter(op = ArchOpEnum.IN_VOLUME)
  public R<List<InVolumeVO>> inVolume(@Validated @RequestBody VolumeBuildUpDTO req) {
    return R.success(arrangeCommonSvc.inVolume(req));
  }

  /** 盒分组字段 */
  @Operation(summary = "获取盒分组字段信息")
  @PostMapping("/boxGroupField")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "获取盒分组字段信息")
  @SaCheckPermission("arrange:common:boxGroupField")
  public R<List<ArchiveRuleFieldListVO>> boxGroupField(
      @Validated @RequestBody BoxGroupFieldQueryDTO req) {
    return R.success(arrangeCommonSvc.boxGroupFieldList(req));
  }

  /** 打散 */
  @Operation(summary = "打散")
  @PostMapping("/breakUp")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.HIGH_RISK,
      title = "打散")
  @SaCheckPermission("arrange:common:breakUp")
  public R<Object> breakUp(@Validated @RequestBody BreakUpDTO req) {
    arrangeCommonSvc.breakUp(req);
    return R.success();
  }

  /** 合并组件 */
  @Operation(summary = " 合并组件")
  @PostMapping("/mergeDocument")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.HIGH_RISK,
      title = "合并组件")
  @SaCheckPermission("arrange:common:mergeDocument")
  @ArchLogParameter(op = ArchOpEnum.COMBINATION)
  public R<Object> mergeDocument(@Validated @RequestBody MergeDocumentDTO req) {
    arrangeCommonSvc.mergeDocument(req);
    return R.success();
  }

  /** 合并组件 */
  @Operation(summary = " 合并组件电子文件列表")
  @PostMapping("/selectMergeFileList")
  @SaCheckPermission("arrange:common:selectMergeFileList")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.HIGH_RISK,
      title = "合并组件电子文件列表")
  public R<List<EfileRecordListVO>> selectMergeFileList(
      @Validated @RequestBody MergeEFileQueryDTO req) {
    return R.success(arrangeCommonSvc.getMergeFileList(req));
  }

  /** 同分类调件 */
  @Operation(summary = " 同分类调件")
  @PostMapping("/changeSort")
  @SaCheckPermission("arrange:common:changeSort")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "同分类调件")
  public R<Object> changeSort(@Validated @RequestBody BaseBO req) {
    arrangeCommonSvc.changeSort(req);
    return R.success();
  }

  /** 同分类调件 */
  @Operation(summary = " 同分类调件列表")
  @PostMapping("/documentChangeSortList")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "同分类调件列表")
  public R<DocumentChangeSortListVO> documentChangeSortList(@Validated @RequestBody Document req) {
    return R.success(arrangeCommonSvc.changeSortList(req));
  }

  /** 同分类调件 */
  @Operation(summary = " 同分类调件列表")
  @PostMapping("/volumeChangeSortList")
  @SaCheckPermission("arrange:common:volumeChangeSortList")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "同分类调件列表")
  public R<VolumeChangeSortListVO> volumeChangeSortList(@Validated @RequestBody Volume req) {
    return R.success(arrangeCommonSvc.changeSortList(req));
  }

  /** 关联档案 按数据 */
  @Operation(summary = "关联档案-按数据")
  @PostMapping("/associateWithData")
  @SaCheckPermission("arrange:common:associateWithData")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "关联档案-按数据")
  public R<Object> associateWithData(@Validated @RequestBody AssociatedWithDataDTO req) {
    arrangeCommonSvc.associatedWithData(req);
    return R.success();
  }

  /** 关联档案 按元数据 */
  @Operation(summary = "关联档案-按元数据")
  @PostMapping("/associateWithMeta")
  @SaCheckPermission("arrange:common:associateWithMeta")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "关联档案-按元数据")
  public R<Object> associateWithMeta(@Validated @RequestBody AssociatedWithMetaDTO req) {
    arrangeCommonSvc.associatedWithMeta(req);
    return R.success();
  }

  /** 整理库。归档库列表 */
  @Operation(summary = "门类列表")
  @PostMapping("/archTypeList")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "关联档案-按元数据")
  public R<List<ArrangeArchTypeListVO>> archTypeList(
      @Validated @RequestBody ArrangeArchTypeQueryDTO req) {
    return R.success(arrangeCommonSvc.archTypeList(req));
  }

  /** 获取表单字段 */
  @Operation(summary = "继承数据查询")
  @GetMapping("/getExtendData")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "继承数据查询")
  public R<Map<String, Object>> getExtendData(SystemBO req) {
    return R.success(arrangeCommonSvc.getExtendData(req));
  }

  /** 获取表单字段 */
  @Operation(summary = "刷新目录树")
  @GetMapping("/refreshCatalog")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "刷新目录树")
  public R refreshCatalog(Catalog req) {
    arrangeCommonSvc.refreshCatalog(req);
    return R.success();
  }

  /** 案卷最大流水号 */
  @Operation(summary = "案卷最大流水号")
  @PostMapping("/getMaxVolumeSequence")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "案卷最大流水号")
  public R<Integer> getMaxVolumeSequence(@Validated @RequestBody MaxSequenceDTO req) {
    return R.success(arrangeCommonSvc.getMaxVolumeSequence(req.getCondition(), req.getTableId()));
  }

  /** 案卷最大流水号 */
  @Operation(summary = "文件最大流水号")
  @PostMapping("/getMaxDocumentSequence")
  @AutoLog(module = "档案整理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "文件最大流水号")
  public R<Integer> getMaxDocumentSequence(@Validated @RequestBody MaxSequenceDTO req) {
    return R.success(arrangeCommonSvc.getMaxDocumentSequence(req.getCondition(), req.getTableId()));
  }

  /** 创建索引 */
  @Operation(summary = "创建索引")
  @PostMapping("/createEsIndex")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.HIGH_RISK,
      title = "创建索引")
  public R createEsIndex(@Validated @RequestBody CreateEsIndexDTO req) {
    arrangeCommonSvc.createEsIndex(req);
    return R.success();
  }

  /** 盒分组字段 */
  @Operation(summary = "获取分组字段信息")
  @PostMapping("/ruleField")
  @AutoLog(
      module = "档案整理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.LOW,
      title = "获取分组字段信息")
  //  @SaCheckPermission("arrange:common:groupField")
  public R<List<ArchiveRuleFieldListVO>> boxGroupField(
      @Validated @RequestBody RuleFieldQueryDTO req) {
    return R.success(arrangeCommonSvc.getRuleFieldByVersion(req));
  }
}
